package com.yyplatform.admin.platform.role.repository;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.yyplatform.admin.platform.role.model.entity.SysUserTokenEntity;

/**
 * Created on 2024-09-14
 */
public interface SysUserTokenRepository extends JpaRepository<SysUserTokenEntity, Long>,
        JpaSpecificationExecutor<SysUserTokenEntity> {
    SysUserTokenEntity findByToken(String token);

    SysUserTokenEntity findByUserId(Long userId);


    @Modifying
    @Query(value = "DELETE FROM sys_user_token WHERE user_id = ?1 ", nativeQuery = true)
    void deleteByUserId(Long id);


    @Query(
            value =
                    "SELECT * FROM sys_user_token WHERE 1=1 "
                            + " AND if(:username != '' , `username` LIKE concat('%', :username, '%'), 1=1 )  "
                            + " AND  (create_time >= :begin AND create_time <= :end ) ",
            countQuery =
                    "SELECT count(id) FROM sys_user_token WHERE 1=1 "
                            + " AND if(:username != '' , `username` LIKE concat('%', :username, '%'), 1=1 )  "
                            + " AND (create_time >= :begin AND create_time <= :end ) ",
            nativeQuery = true)
    Page<SysUserTokenEntity> findPage(
            @Param("username") String username,
            @Param("begin") Date begin,
            @Param("end") Date end,
            @Param("pageRequestOrderByIdDesc") PageRequest pageRequestOrderByIdDesc);


}
